package com.xique.park.service.biz.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.park.bean.domain.ParkCar;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.bean.request.web.parkcar.BlackCarRequest;
import com.xique.park.bean.request.web.parkcar.ParkCarRequest;
import com.xique.park.bean.vo.app.ParkCarVo;
import com.xique.park.bean.vo.comm.MngTypeVo;
import com.xique.park.service.biz.service.IParkCarApiService;
import com.xique.park.service.biz.service.IParkCarBizService;
import com.xique.park.service.command.service.IParkCarCommandService;
import com.xique.park.service.service.IParkCarMngtypeService;
import com.xique.park.service.service.IParkCarService;
import com.xique.park.service.service.IParkInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: caogq
 * @Date: 2022/6/17 15:43
 * @Description: 对外接口-车辆管理
 */
@Service
@Slf4j
public class ParkCarApiServiceImpl implements IParkCarApiService {

    @Autowired
    private IParkCarService parkCarService;

    @Autowired
    private IParkCarBizService parkCarBizService;

    @Autowired
    private IParkInfoService parkInfoService;

    @Autowired
    private IParkCarCommandService parkCarCommandService;

    @Autowired
    private IParkCarMngtypeService parkCarMngtypeService;

    /**
     * 删除车辆
     *
     * @param carId  车辆id
     * @param parkId 车场id
     * @return
     */
    @Override
    @Transactional
    public AjaxResult removeCar(Long carId, Long parkId) {
        //1、查询该车场下是否拥有该车辆
        ParkCar parkCar = new ParkCar();
        parkCar.setId(carId);
        parkCar.setParkId(parkId);
        List<ParkCar> parkCars = parkCarService.selectParkCarList(parkCar);
        if (parkCars.isEmpty()) {
            return AjaxResult.error("当前车场不存在该车辆");
        }
        // 下发到设备
        return parkCarCommandService.delSingleCar(parkId, parkCars.get(0), "删除中！");
    }

    @Override
    public AjaxResult getCarList(ParkCarRequest request) {
        PageResult pageResult = parkCarService.selectPageList(request);
        List<ParkCarVo> parkCarVoList = (List<ParkCarVo>) pageResult.getList();
        Map<String, Object> map = new HashMap<>(3);
        map.put("list", parkCarVoList);
        map.put("total", pageResult.getTotalPages());
        map.put("totalSize", pageResult.getTotalSize());
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult getMngTypeList(Long parkId) {
        ParkCarMngtype parkCarMngtype = new ParkCarMngtype();
        parkCarMngtype.setParkId(parkId);
        List<ParkCarMngtype> list = parkCarMngtypeService.selectParkCarMngtypeList(parkCarMngtype);
        Map<String, Object> map = new HashMap<>(1);
        List<MngTypeVo> vos = list.stream().map(result -> {
            MngTypeVo vo = new MngTypeVo();
            vo.setMngTypeId(result.getId());
            vo.setMngTypeName(result.getName());
            vo.setMngType(result.getMngType());
            return vo;
        }).collect(Collectors.toList());
        map.put("list", vos);
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult addBlack(BlackCarRequest request) {
        ParkCar search = new ParkCar();
        search.setParkId(request.getParkId());
        search.setId(request.getId());
        List<ParkCar> list = parkCarService.selectParkCarList(search);
        if (CollectionUtil.isEmpty(list)) {
            return AjaxResult.error("该车辆不属于当前车场");
        }

        return parkCarService.saveBlackCar(request);
    }
}
